/**
 * @author yaoyuan
 * @version 1.0
 * @name zjzcVotingApp.Directive:directive
 * @description app 指令集合
 */

'use strict';

angular.module('app.directive', [])
    .directive('appDropdown', function() {

            return function(scope, element, attrs) {
                element.children('a').bind('click', function(event) {
                    if(!jQuery(this).next().is(':visible'))
                        jQuery(this).next().slideDown('fast');
                    else
                        jQuery(this).next().slideUp('fast');
                    return false;
                });
            }
    })

    .directive('appUniform', function() {

            return function(scope, element, attrs) {
               // jQuery('input:checkbox, input:radio, .uniform-file').uniform();
            }
    })

    .directive('topbar', function() {
        return function(scope, element, attrs) {
            if(!jQuery('.topbar').length) {
                // add menu bar for phones and tablet
               jQuery('<div class="topbar"><a class="barmenu">'+'</a></div>').insertBefore(element);
            }
            jQuery('.topbar .barmenu').click(function() {
                  var lwidth = '260px';
                  if(jQuery(window).width() < 340) {
                             lwidth = '240px';
                  }
                  if(!jQuery(this).hasClass('open')) {
                             jQuery('.rightpanel, .headerinner, .topbar').css({marginLeft: lwidth},'fast');
                             jQuery('.logo, .leftpanel').css({marginLeft: 0},'fast');
                             jQuery(this).addClass('open');
                  } else {
                             jQuery('.rightpanel, .headerinner, .topbar').css({marginLeft: 0},'fast');
                             jQuery('.logo, .leftpanel').css({marginLeft: '-'+lwidth},'fast');
                             jQuery(this).removeClass('open');
                  }
            });
            // show/hide left menu
            jQuery(window).resize(function(){
                  if(!jQuery('.topbar').is(':visible')) {
                         jQuery('.rightpanel, .headerinner').css({marginLeft: '260px'});
                            jQuery('.logo, .leftpanel').css({marginLeft: 0});
                  } else {
                         jQuery('.rightpanel, .headerinner').css({marginLeft: 0});
                            jQuery('.logo, .leftpanel').css({marginLeft: '-260px'});
                  }
           });
        }
    })
    //------------------ 图片刷新  -----------------------
    .directive('vcodeFresh', function() {
            var  src = server+"/validcode/generate-validcode.do";
            return function(scope, element, attrs) {
                var _reload = function(dom) {
                    // 随机串，避免验证码 IE 缓存
                    dom.src = src + '?_v=' + new Date().getTime();
                }
                element.bind("click", function(event) {
                    _reload(this);
                })
                _reload(element[0]);
            }
    })
    .directive('appCopy', function() {
            return function(scope, element, attrs) {
                jQuery("body")
                      .on("copy", ".btn-file", function(/* ClipboardEvent */ e) {
                        e.clipboardData.clearData();
                        e.clipboardData.setData("text/plain", 'asdasd');
                        e.preventDefault();
                      });
            }
    })
    //重写(日历my97)datepicker指令,原指令封装造成不能复杂设置
    .directive('ngDatepicker',function(){//在angular ui-bootstrap中指令ngDatepicker已经存在,不要引入,以免冲突
        var dataPicker = {
            restrict : 'A',
            require : '?ngModel',
            link: function(scope, element, attrs, ngModel){
                if (!ngModel) {
                    return;
                }

                //使用my97datepicker4.7版本会出现元素焦点状态,隔次才会弹出的bug
                element.on("click",function(){

                    //默认参数(设置属性要在事件中获取,否则无法同步angular双向绑定数据)
                    var options = {
                        el: this,
                        onpicked: function(){
                            scope.$apply(function() {
                                ngModel.$setViewValue(element.val());
                            });

                            /*这里获取不到optins其他值
                            if (angular.isDefined(options.triggerOther)){
                                document.querySelector(options.triggerOther).click();
                            }*/
                        }
                    }
                    //属性参数处理(使用注意对应驼峰命名),triggerOther自定义属性,选择第一个日期,第二个日期自动弹出,通过querySelector获取dom对象
                    angular.forEach([ 'skin', 'dateFmt', 'minDate', 'maxDate', 'startDate', 'isShowClear', 'isShowOK', 'isShowToday', 'readOnly', 'disabledDays', 'disabledDates', 'triggerOther'], function(key) {
                        if (angular.isDefined(attrs[key])) options[key] = attrs[key];
                    });

                    WdatePicker(options);
                });
            }
        }

        return dataPicker;
    });

